<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        /* 整体页面样式 */
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f8f9fa;
            margin: 0;
            padding: 0;
        }

        .container {
            width: 90%;
            margin: 30px auto;
            background-color: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        h2 {
            margin-bottom: 20px;
            text-align: center;
            color: #333;
        }

        /* 搜索栏样式 */
        .search-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .search-bar input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        .search-bar button {
            padding: 10px 20px;
            background-color: #28a745;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .search-bar button:hover {
            background-color: #218838;
        }

        /* 表格样式 */
        .student-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .student-table th,
        .student-table td {
            padding: 12px;
            text-align: center;
            border-bottom: 1px solid #ddd;
        }

        .student-table th {
            background-color: #f2f2f2;
            font-weight: bold;
        }

        /* 操作按钮样式 */
        .action-buttons {
            display: flex;
            justify-content: center;
            gap: 5px;
        }

        .action-buttons button {
            padding: 5px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .action-buttons button:nth-child(1) {
            background-color: #28a745;
            color: white;
        }

        .action-buttons button:nth-child(2) {
            background-color: #dc3545;
            color: white;
        }

        .action-buttons button:hover {
            opacity: 0.8;
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
        }

        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 300px;
            border-radius: 8px;
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }

        .close:hover {
            color: black;
        }

        /* 表单样式 */
        .modal-content form {
            display: flex;
            flex-direction: column;
        }

        .modal-content form label {
            margin-bottom: 5px;
        }

        .modal-content form input {
            padding: 8px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        .modal-content form button {
            padding: 10px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .modal-content form button:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>  </h2>
    <div class="search-bar">
        <input type="text" id="searchInput" placeholder="请输入用户名搜索...">
        <button onclick="searchStudents()">查询</button>
        <button onclick="showAddModal()">添加学生</button>
    </div>

    <!-- 新增/编辑弹窗 -->
    <div class="modal" id="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal()">&times;</span>
            <h3 id="modalTitle">添加学生</h3>
            <form id="studentForm">
                <input type="hidden" id="studentNo" name="studentNo">
                <label>姓名：</label>
                <input type="text" id="name" name="name" required><br>
                <label>性别（1-男，0-女）：</label>
                <input type="text" id="gender" name="gender" required><br>
                <label>生日：</label>
                <input type="date" id="birthday" name="birthday" required><br>
                <button type="button" onclick="saveStudent()">保存</button>
            </form>
        </div>
    </div>

    <!-- 学生列表展示表格 -->
    <table class="student-table">
        <thead>
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>生日</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="studentTableBody">
        <!-- 这里动态加载学生数据 -->
        </tbody>
    </table>
</div>

<script>
    // 加载学生列表，可根据搜索条件加载，这里简单处理为加载全部
    function loadStudents() {
        // 发起 AJAX GET 请求获取所有学生数据
        fetch('/student/list')
            .then(response => response.json())
            .then(students => {
                const tbody = document.getElementById('studentTableBody');
                tbody.innerHTML = '';
                students.forEach(student => {
                    const row = document.createElement('tr');
                    row.innerHTML = `
                        <td>${student.studentNo}</td>
                        <td>${student.name}</td>
                        <td>${student.gender === 1 ? '男' : '女'}</td>
                        <td>${student.birthday}</td>
                        <td>
                            <div class="action-buttons">
                                <button onclick="showEditModal(${student.studentNo})">修改</button>
                                <button onclick="deleteStudent(${student.studentNo})">删除</button>
                            </div>
                        </td>
                    `;
                    tbody.appendChild(row);
                });
            })
            .catch(error => {
                console.error('加载学生列表失败:', error);
                alert('加载学生列表失败，请稍后重试');
            });
    }

    // 搜索学生
    function searchStudents() {
        const searchInput = document.getElementById('searchInput').value;
        if (!searchInput) {
            loadStudents();
            return;
        }

        fetch(`/student/get/{studentNo}?name=${encodeURIComponent(searchInput)}`)
            .then(response => response.json())
            .then(students => {
                const tbody = document.getElementById('studentTableBody');
                tbody.innerHTML = '';
                students.forEach(student => {
                    const row = document.createElement('tr');
                    row.innerHTML = `
                        <td>${student.studentNo}</td>
                        <td>${student.name}</td>
                        <td>${student.gender === 1 ? '男' : '女'}</td>
                        <td>${student.birthday}</td>
                        <td>
                            <div class="action-buttons">
                                <button onclick="showEditModal(${student.studentNo})">修改</button>
                                <button onclick="deleteStudent(${student.studentNo})">删除</button>
                            </div>
                        </td>
                    `;
                    tbody.appendChild(row);
                });
            })
            .catch(error => {
                console.error('搜索学生失败:', error);
                alert('搜索学生失败，请稍后重试');
            });
    }

    // 显示添加学生的模态框
    function showAddModal() {
        document.getElementById('modalTitle').innerText = '添加学生';
        document.getElementById('studentForm').reset();
        document.getElementById('modal').style.display = 'block';
    }

    // 显示编辑学生的模态框，根据学号获取学生信息并填充表单
    function showEditModal(studentNo) {
        fetch(`/student/get/${studentNo}`)
            .then(response => response.json())
            .then(student => {
                document.getElementById('modalTitle').innerText = '修改学生';
                document.getElementById('studentNo').value = student.studentNo;
                document.getElementById('name').value = student.name;
                document.getElementById('gender').value = student.gender;
                document.getElementById('birthday').value = student.birthday;
                document.getElementById('modal').style.display = 'block';
            })
            .catch(error => {
                console.error('获取学生详情失败:', error);
                alert('获取学生详情失败，请稍后重试');
            });
    }

    // 关闭模态框
    function closeModal() {
        document.getElementById('modal').style.display = 'none';
    }

    // 保存学生信息（添加或修改）
    function saveStudent() {
        const studentNo = document.getElementById('studentNo').value;
        const name = document.getElementById('name').value;
        const gender = document.getElementById('gender').value;
        const birthday = document.getElementById('birthday').value;

        const student = {
            studentNo: studentNo ? parseInt(studentNo) : null,
            name: name,
            gender: parseInt(gender),
            birthday: birthday
        };

        let url = '/student/add';
        let method = 'POST';
        if (studentNo) {
            url = '/student/update';
            method = 'PUT';
        }

        fetch(url, {
            method: method,
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(student)
        })
            .then(response => response.json())
            .then(result => {
                alert(result.message);
                if (result.success) {
                    closeModal();
                    loadStudents();
                }
            })
            .catch(error => {
                console.error('保存学生失败:', error);
                alert('保存学生失败，请稍后重试');
            });
    }

    // 删除学生
    function deleteStudent(studentNo) {
        if (!confirm('确定要删除该学生吗？')) {
            return;
        }

        fetch(`/student/delete/${studentNo}`, {
            method: 'DELETE'
        })
            .then(response => response.json())
            .then(result => {
                alert(result.message);
                if (result.success) {
                    loadStudents();
                }
            })
            .catch(error => {
                console.error('删除学生失败:', error);
                alert('删除学生失败，请稍后重试');
            });
    }

    // 页面加载完成后加载学生列表
    window.onload = function () {
        loadStudents();
    };
</script>
</body>
</html>